package com.sctvcloud.dongqu.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;

import com.sctvcloud.dongqu.beans.FocusColumn;
import com.sctvcloud.dongqu.beans.ServiceBean;
import com.sctvcloud.dongqu.ui.fragment.AnchorFragment;
import com.sctvcloud.dongqu.ui.fragment.CityStateFragment;
import com.sctvcloud.dongqu.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.dongqu.ui.fragment.CommonFragment;
import com.sctvcloud.dongqu.ui.fragment.GreatestFragment;
import com.sctvcloud.dongqu.ui.fragment.HighRiseFragment;
import com.sctvcloud.dongqu.ui.fragment.LiveFragment;
import com.sctvcloud.dongqu.ui.fragment.NewsListFragment;
import com.sctvcloud.dongqu.ui.fragment.ServiceColumnFragment;
import com.sctvcloud.dongqu.ui.fragment.SpecialFragment;
import com.sctvcloud.dongqu.ui.fragment.TvFragment;

import java.lang.ref.WeakReference;
import java.util.List;

public class ServicePagerAdapter extends FragmentStatePagerAdapter {
    private List<ServiceBean> serviceTypeList;
    private SparseArray<WeakReference<ServiceColumnFragment>> fragmentWeaks;
    private ServiceColumnFragment.OnServiceUsedListener listener;

    public ServicePagerAdapter(FragmentManager fm, List<ServiceBean> list, ServiceColumnFragment.OnServiceUsedListener listener) {
        super(fm);
        this.listener = listener;
        serviceTypeList = list;
        fragmentWeaks = new SparseArray<>();
    }

    public void setServiceTypeList(List<ServiceBean> serviceTypeList) {
        this.serviceTypeList = serviceTypeList;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        ServiceColumnFragment fragment = new ServiceColumnFragment();
        fragment.setServiceList(serviceTypeList.get(position).getServiceData());
        fragment.setServiceUsedListener(listener);
        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ServiceColumnFragment>(fragment));
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return serviceTypeList.get(position).getServiceName();
    }

    @Override
    public int getCount() {
        return serviceTypeList.size();
    }

}
